package org.hot100.h101;

/**
 * @Author: wjy
 * @Date: 2021/12/29 10:18
 */
public class Solution {

    /**
     * 给定一个二叉树，检查它是否是镜像对称的。
     *
     * 例如，二叉树 [1,2,2,3,4,4,3] 是对称的。
     *
     * 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
     * @param args
     */
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(3);
        TreeNode treeNode2 = new TreeNode(4);
        TreeNode treeNode3 = new TreeNode(2, treeNode1, treeNode2);
        TreeNode treeNode4 = new TreeNode(4);
        TreeNode treeNode5 = new TreeNode(3);
        TreeNode treeNode6 = new TreeNode(3, treeNode4, treeNode5);
        TreeNode treeNode7 = new TreeNode(1, treeNode3, treeNode6);

        Solution solution = new Solution();
        System.out.println(solution.isSymmetric(treeNode7));

    }

    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return false;
        }

        if (root.left != null || root.right != null) {
            return this.check(root.left, root.right);
        }
        return true;
    }

    public boolean check(TreeNode l, TreeNode r) {
        if (l == null && r == null) {
            return true;
        }
        if (l == null || r == null || l.val != r.val) {
            return false;
        }

        return this.check(l.left, r.right) && this.check(l.right, r.left);
    }

}
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}